package com.echo.code.thread.mode.active;

import com.echo.code.thread.future.Future;
import com.echo.code.thread.future.impl.SimpleFuture;
import java.util.Collections;
import java.util.Map;
import java.util.stream.IntStream;

/**
 * UserServiceImpl Created by baiyap on 2018/10/6.
 */
public class ServiceImpl implements Service {

  private final static ThreadLocal<Map<Integer, String>> THREAD_LOCAL = new ThreadLocal<>();

  @ActiveMethod
  @Override
  public void insert(int id, String name) {
    IntStream.range(0, 3000000).mapToObj(String::valueOf).forEach(s -> System.out.println("insert ... " + s));
    THREAD_LOCAL.set(Collections.singletonMap(id, name));
  }

  @ActiveMethod
  @Override
  public Future<String> getName(int id) {
    IntStream.range(0, 3000000).mapToObj(String::valueOf).forEach(s -> System.out.println("getName ... " + s));
    Future<String> future = new SimpleFuture<>();
    future.finish(THREAD_LOCAL.get().getOrDefault(id, "empty"));
    return future;
  }
}
